package com.mantru.iotpot.Cleaning;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.mantru.iotpot.ApplicationClass;
import com.mantru.iotpot.DateClass.Cleaning_List;
import com.mantru.iotpot.R;
import com.mantru.iotpot.Util.ToastUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class PotCuringActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView title;
    private TextView wash_time;
    private TextView wash_weight;
    private TextView heat_time;
    private Button back;
    private RelativeLayout box;
    private ApplicationClass ac;
    private String TAG = "PotCuringActivity";
    private List<Cleaning_List> cleaningLists = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pot_curing);
        ac = (ApplicationClass) getApplication();// 初始化 ApplicationClass
        initialization();//初始化控件
        getJson();//获取json数据
        setview();//设置按钮文本
    }

    //初始化控件
    private void initialization(){
        title = findViewById(R.id.pot_curing_wash_name1);
        wash_time = findViewById(R.id.pot_curing_wash_time1);
        wash_weight = findViewById(R.id.pot_curing_wash_water1);
        heat_time = findViewById(R.id.pot_curing_wash_hot1);
        box = findViewById(R.id.pot_curing_wash_button1);
        back = findViewById(R.id.pot_curing_wash_back);
        back.setOnClickListener(this);
        box.setOnClickListener(this);
    }

    private void getJson(){
        String json_data = ac.GetSQLData_Bringup_List();
        Log.d(TAG,"json_data: " + json_data);
        try {
            JSONObject jsonObject = new JSONObject(json_data);
            boolean success = jsonObject.getBoolean("success");
            if (success){
                JSONArray jsonArray = jsonObject.getJSONArray("item_data");
                for (int i = 0;i < jsonArray.length();i++){
                    JSONObject cleaningJson = jsonArray.getJSONObject(i);
                    String type = cleaningJson.getString("type");
                    String name = cleaningJson.getString("name");
                    String time = cleaningJson.getString("time");
                    String oil = cleaningJson.getString("oil");
                    String hot = cleaningJson.getString("hot");
                    cleaningLists.add(new Cleaning_List(type,name,time,oil,hot));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            Log.d(TAG,"解析json数据失败");
            ac.SaveLog(TAG + "解析json数据失败,失败原因: " + e);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.pot_curing_wash_back){//返回按钮
            finish();
        } else if (v.getId() == R.id.pot_curing_wash_button1) {//选择的养锅模式
//            resetButtons();//清除所有按钮背景色
//            box.setBackgroundResource(R.drawable.button12);
//            Intent intent = new Intent(this, PotCuringWaitActivity.class);
//            intent.putExtra("oil_type", 1);
//            startActivity(intent);
//            finish();
            ToastUtils.show(this,"此功能还在开发中");
        }
    }

    //设置按钮文本
    public void setview() {
        if (!cleaningLists.isEmpty()) {
            for (Cleaning_List clean : cleaningLists) {
                if (clean.getCleaning_type().equals("1")) {
                    title.setText(clean.getCleaning_name());
                    wash_time.setText(clean.getCleaning_time() + "s");
                    wash_weight.setText(clean.getCleaning_water() + "g油");
                    if (clean.getCleaning_hot().equals("0")){
                        heat_time.setText("不加热");
                    }else {
                        heat_time.setText("加热" + clean.getCleaning_hot() + "s");
                    }
                } else if (clean.getCleaning_type().equals("2")) {

                } else if (clean.getCleaning_type().equals("3")) {

                } else if (clean.getCleaning_type().equals("4")) {

                }
            }
        }
    }

    private void resetButtons() {//移除所有按钮的样式
        box.setBackgroundResource(R.drawable.box);
//        button2.setBackgroundResource(R.drawable.box);
//        button3.setBackgroundResource(R.drawable.box);
//        button4.setBackgroundResource(R.drawable.box);
    }
}